#include "Sprite.h"

Sprite::Sprite()
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SDL_Rect emptyRect = { 0 };
	SetSrcRect(emptyRect);
	SetDstRect(emptyRect);
	collider = new Circle();
}

Sprite::Sprite(const std::string& path)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(srcRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, int x, int y)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = x;
	dstRect.y = y;
	dstRect.w = srcRect.w;
	dstRect.h = srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, const Vector* const v)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = (int)(v->x);
	dstRect.y = (int)(v->y);
	dstRect.w = srcRect.w;
	dstRect.h = srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, int x, int y, int w, int h)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = x;
	dstRect.y = y;
	dstRect.w = w;
	dstRect.h = h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, const Vector* const v, int w, int h)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = (int)(v->x);
	dstRect.y = (int)(v->y);
	dstRect.w = w;
	dstRect.h = h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, const SDL_Rect& dstRect)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);
	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(const std::string& path, const SDL_Rect& srcRect, const SDL_Rect& dstRect)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(path);
	SetSrcRect(srcRect);
	SetDstRect(dstRect);
	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(SDL_Texture* tex)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(tex);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(srcRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(SDL_Texture* tex, int x, int y)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(tex);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = x;
	dstRect.y = y;
	dstRect.w = srcRect.w;
	dstRect.h = srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(SDL_Texture* tex, int x, int y, int w, int h)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(tex);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SDL_Rect dstRect;
	dstRect.x = x;
	dstRect.y = y;
	dstRect.w = w;
	dstRect.h = h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);

	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(SDL_Texture* tex, const SDL_Rect& rect)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(tex);

	SDL_Rect srcRect;
	srcRect.x = 0;
	srcRect.y = 0;
	srcRect.w = this->srcRect.w;
	srcRect.h = this->srcRect.h;

	SetSrcRect(srcRect);
	SetDstRect(dstRect);
	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::Sprite(SDL_Texture* tex, const SDL_Rect& dstRect, const SDL_Rect& srcRect)
	: Component()
	, tex(0)
	, isVisible(true)
	, isCircular(true)
	, angle(0.0f)
	, collider(0)
	, flipState(SDL_FLIP_NONE)
{
	SetTexture(tex);
	SetSrcRect(srcRect);
	SetDstRect(dstRect);
	collider = new Circle((float)(dstRect.x + dstRect.w / 2), (float)(dstRect.y + dstRect.h / 2), (float)dstRect.w / 2);
}

Sprite::~Sprite()
{
	delete collider, tex;
}

void Sprite::Start()
{
}

void Sprite::Update(float dt)
{
}

void Sprite::Draw()
{
	if (tex && isVisible)
	{
		ApplySurface(Engine::GetInstance()->GetRenderer());
	}
}

void Sprite::Stop()
{
}

void Sprite::ApplySurface(SDL_Renderer* renderer)
{	
	SDL_RenderCopyEx(renderer, tex, &srcRect, &dstRect, angle, 0, flipState);
}